from Case.python_ui.Common.basepage import Basepage
import time,pytest
from Case.python_ui.PageLocate.organization_locator import OrganizationLocator
from Case.python_ui.Test_data.login_data import Logindata
from Case.python_ui.Pageobject.organization_pages import OrganizationPage
from Case.python_ui.Pageobject.login_pages import Loginpages
from Case.python_ui.PageLocate.role_locator import Rolelocator
from Case.python_ui.Common.do_mysql import DoMysql
from Case.python_ui.Test_data.role_data import Role
from Case.python_ui.Test_data.organization_data import Organization
from Case.python_ui.Common.commontool import CommonTool
from Case.python_ui.PageLocate.user_locator import UserLocator
from Case.python_ui.Test_data.user_data import User
from Case.python_ui.Test_data.common_data import Commondata
from bs4 import BeautifulSoup
from Case.python_ui.PageLocate.login_locator import Loginlocator
from selenium.common.exceptions import StaleElementReferenceException
from Case.python_ui.PageLocate.usergroup_locator import UserGroupLocator
from Case.python_ui.Common.business_common_tool import BusinessCommonTools
from selenium.common.exceptions import TimeoutException,NoSuchElementException
import re


class UserPage(Basepage):

    """
        公共方法
    """
    # 等待提交成功提示
    def wait_web_success_prompt(self, time=100):
        doc = '等待页面提交成功提示'
        try:
            self.wait_elevisble(Rolelocator.add_success_prompt, doc,time)
            self.wait_eleinvisble(Rolelocator.add_success_prompt, doc,time)
        except:
            pass

    """
    
    """
    #进入到user页面
    def enter_the_user_page(self):
        doc='进入到user主页'
        time.sleep(5)
        for i in range(3):
            try:
                self.wait_elevisble(OrganizationLocator.user_management_tab,doc)
                self.move_mouse_to_click_element(OrganizationLocator.user_management_tab,doc)
                time.sleep(5)
                self.wait_elevisble(UserLocator.user_tab,doc)
                self.move_mouse_to_click_element(UserLocator.user_tab,doc)
                break
            except:
                self.driver.refresh()
                time.sleep(5)
                continue
        try:
            self.wait_elevisble(UserLocator.user_page_loading, doc)
            self.wait_eleinvisble(UserLocator.user_page_loading, doc)
        except:
            pass
        time.sleep(2)
        self.refresh_email_or_phone()
        try:
            self.reset_filtering()
            time.sleep(2)
            self.reset_dunning_filter()
        except:
            print('进入到user主页的异常')
            pass
        time.sleep(10)
        self.wait_elevisble(UserLocator.page_total,doc)



    #创建一个user
    def  add_new_member(self,org_name,cmdopt):
         time.sleep(5)
         doc='创建一个user'
         self.refresh_user_page(Commondata.home_page[cmdopt]['user'])
         self.enter_the_user_page()
         time.sleep(2)
         # for i in range(3):
         #    try:
         #        self.wait_elevisble(UserLocator.user_page_loading,doc)
         #        self.wait_eleinvisble(UserLocator.user_page_loading,doc)
         #        self.click_element(UserLocator.invite_new_user,doc)
         #        time.sleep(5)
         #        break
         #    except:
         #        self.driver.refresh()
         #        time.sleep(5)
         #        continue
         self.wait_elevisble(UserLocator.invite_new_user, doc)
         self.click_element(UserLocator.invite_new_user, doc)
         time.sleep(2)
         email=CommonTool.generate_email()
         self.wait_elevisble(UserLocator.new_user_email,doc)
         self.send_keys(UserLocator.new_user_email,email,doc)
         self.click_element(UserLocator.user_role_choose_list,doc)
         time.sleep(2)
         self.save_screen(doc + '点选role之前')
         self.wait_elevisble(UserLocator.default,doc)
         self.click_element(UserLocator.default,doc)
         self.send_keys(UserLocator.org_option,org_name,doc)
         time.sleep(2)
         self.save_screen(doc+'点选机构之前')
         self.wait_elevisble(UserLocator().org_option_choose(org_name),doc)
         self.move_mouse_to_click_element(UserLocator().org_option_choose(org_name),doc)
         time.sleep(2)
         self.click_element(UserLocator.invite_new_user_ok_button,doc)
         #self.wait_elevisble(Rolelocator.add_success_prompt,doc)
         self.wait_web_success_prompt(2)
         #self.wait_eleinvisble(OrganizationLocator.loading_wait,doc)
         #self.query_user(email)
         #eles= self.get_elements(UserLocator.user_default_role,doc)
         #role_name=eles[-1].text
         return email

#删除user
    def delete_user(self,email,cmdopt):
        doc='删除创建的user'
        time.sleep(5)
        self.refresh_user_page(Commondata.home_page[cmdopt]['user'])
        self.enter_the_user_page()
        self.query_user(email)
        for i in range(3):
            try:
                time.sleep(2)
                self.move_mouse_to_element(UserLocator.action_button)
                time.sleep(2)
                self.get_operation_name_click('Delete')
                time.sleep(2)
                self.wait_elevisble(UserLocator.delete_acknowledge, doc)
                break
            except TimeoutException as e:
                self.save_screen('异常中再次点击action')
                continue
            except NoSuchElementException as e:
                self.save_screen('异常中再次点击action')
                continue
        time.sleep(2)
        self.click_element(UserLocator.delete_acknowledge,doc)
        time.sleep(2)
        self.wait_elevisble(UserLocator.delete_confirm,doc)
        time.sleep(2)
        self.click_element(UserLocator.delete_confirm,doc)
        time.sleep(2)
        self.wait_elevisble(OrganizationLocator.add_success_prompt,doc)
        #self.click_element(UserLocator.account_phone_query_button)
        time.sleep(2)

    #查询user
    def query_user(self,email):
        doc='查询user'
        self.reset_filtering()
        time.sleep(5)
        self.reset_dunning_filter()
        time.sleep(2)
        self.refresh_email_or_phone()
        time.sleep(2)
        self.wait_elevisble(UserLocator.account_phone_query_input,doc)
        #self.keyboard_event(UserLocator.account_phone_query_input,doc)
        time.sleep(2)
        self.send_keys(UserLocator.account_phone_query_input, email,doc)
        self.move_mouse_to_click_element(UserLocator.account_phone_query_button,doc)
        self.wait_elevisble(UserLocator.query_result_element,doc)
        time.sleep(8)





    def user_page_field_validation(self,cmdopt):
         doc='进入到组织页面并且查看页面字段'
         time.sleep(5)
         self.refresh_user_page(Commondata.home_page[cmdopt]['user'])
         self.enter_the_user_page()



    def search_field_retrieval_query(self,email,cmdopt):
        doc='筛选状态查询'
        self.user_page_field_validation(cmdopt)
        self.query_user(email)
        time.sleep(2)
        self.move_mouse_to_click_element(UserLocator.account_phone_query_button,doc)
        time.sleep(5)
        self.wait_elevisble(UserLocator.get_account,doc)
        res=self.get_element(UserLocator.get_account,doc).get_attribute('innerHTML')
        print(res)
        return  res

    # 20241031 查询列表第一条数据的email，无值返回0
    def get_first_email(self):
        doc = '获取user management | user下第一条数据的email'
        try:
            self.wait_elevisble(UserLocator.get_account,doc)
            first_email=self.get_element(UserLocator.get_account,doc).text
            return first_email
        except:
            return 0

    # 20241031 返回列表数据量，无值返回0
    def get_num_total(self):
        doc = '返回列表数据量'
        try:
            self.wait_elevisble(UserLocator.num_total,doc)
            total_string=self.get_element(UserLocator.num_total,doc).text
            # 使用正则表达式模式对字符串进行拆分
            pattern = r"[ ]"
            total_num_list = re.split(pattern, total_string)
            return int(total_num_list[1])
        except:
            return 0

    def query_non_existent_accounts(self,cmdopt):
        doc='查询不存在的用户'
        self.user_page_field_validation(cmdopt)
        self.query_user(User.non_existent_email)
        res=self.get_elements(UserLocator.get_account,doc)
        return  res

    def page_filtering_function_retrieval(self,cmdopt):
        doc='筛选查询'
        self.user_page_field_validation(cmdopt)
        time.sleep(2)
        self.wait_elevisble(UserLocator.status_filter,doc)
        time.sleep(2)
        self.move_mouse_to_click_element(UserLocator.status_filter,doc)
        time.sleep(2)
        self.wait_elevisble(UserLocator.filter_normal,doc)
        self.move_mouse_to_click_element(UserLocator.filter_normal,doc)
        self.click_element(UserLocator.filter_ok,doc)
        self.wait_elevisble(UserLocator.status,doc)
        ele=self.get_elements(UserLocator.status,doc)
        status=ele[0].text
        return status


    def reset_filtering(self):
        doc='重置筛选项'
        time.sleep(2)
        self.wait_elevisble(UserLocator.status_filter, doc)
        self.move_mouse_to_click_element(UserLocator.status_filter, doc)
        self.save_screen(doc+'-点击status按钮后')
        time.sleep(2)
        self.wait_elevisble(UserLocator.filter_reset_button,doc)
        self.move_mouse_to_click_element(UserLocator.filter_reset_button,doc)
        time.sleep(1)
        self.move_mouse_to_click_element(UserLocator.filter_ok,doc)
        time.sleep(1)

    # 重制查询条件
    def refresh_email_or_phone(self):
        doc = '重置筛查询条件'
        self.wait_elevisble(UserLocator.account_phone_query_input,doc)
        self.double_click(UserLocator.account_phone_query_input,doc)
        self.keyboard_event_all_select(UserLocator.account_phone_query_input,doc)
        self.keyboard_event(UserLocator.account_phone_query_input,doc)
        time.sleep(1)
        self.move_mouse_to_click_element(UserLocator.account_phone_query_button, doc)
        time.sleep(5)



    def filter_dunning_stastus(self,cmdopt):
        doc='dunningstatus状态查询'
        self.user_page_field_validation(cmdopt)
        for i in range(3):
            try:
                time.sleep(5)
                self.move_mouse_to_click_element(UserLocator.dunning_status_filter,doc)
                time.sleep(2)
                self.wait_elevisble(UserLocator.filter_normal,doc)
                self.move_mouse_to_click_element(UserLocator.filter_normal,doc)
                break
            except TimeoutException as e:
                continue
            except NoSuchElementException as e:
                continue
        # time.sleep(5)
        # self.wait_elevisble(UserLocator.dunning_status_filter,doc)
        # self.move_mouse_to_click_element(UserLocator.dunning_status_filter,doc)
        # time.sleep(3)
        # self.wait_elevisble(UserLocator.filter_normal,doc)
        # self.move_mouse_to_click_element(UserLocator.filter_normal,doc)
        time.sleep(2)
        self.wait_elevisble(UserLocator.filter_ok,doc)
        self.move_mouse_to_click_element(UserLocator.filter_ok,doc)
        time.sleep(2)
        self.wait_elevisble(UserLocator.status,doc)
        ele = self.get_elements(UserLocator.status,doc)
        print(ele[1].text)
        status = ele[1].text
        return status

    def reset_dunning_filter(self):
        doc='重置dunningstatus状态'
        time.sleep(2)
        self.move_mouse_to_click_element(UserLocator.dunning_status_filter,doc)
        time.sleep(2)
        self.save_screen(doc+'reset')
        self.wait_elevisble(UserLocator.dunning_filter_reset_button, doc)
        self.move_mouse_to_click_element(UserLocator.dunning_filter_reset_button, doc)
        time.sleep(2)
        self.save_screen(doc+'ok')
        self.move_mouse_to_click_element(UserLocator.filter_ok, doc)
        time.sleep(1)
        try:
            self.wait_elevisble(UserLocator.user_page_loading, doc)
            self.wait_eleinvisble(UserLocator.user_page_loading, doc)
        except:
            pass


    def get_acccount_email(self):
         doc='获取账号'
         self.wait_elevisble(UserLocator.account_email,doc)
         account_email = self.get_element(UserLocator.account_email,doc).text
         time.sleep(5)
         return account_email

    def get_status_name(self):
         doc='获取状态'
         time.sleep(2)
         self.move_mouse_to_click_element(UserLocator.status_filter,doc)
         status_list = []
         status = self.get_elements(UserLocator.status_names,doc)
         for i in status:
              status_list.append(i.get_attribute('innerHTML'))
         time.sleep(5)
         return status_list

    def  get_dunning_status_name(self):
         doc='获取dunning状态'
         self.move_mouse_to_click_element(UserLocator.dunning_status_filter,doc)
         time.sleep(2)
         self.wait_eleinvisble(OrganizationLocator.loading_wait,doc)
         self.click_element(UserLocator.dunning_status_filter,doc)
         time.sleep(4)
         dunning_status_list = []
         dunning_status_names=self.get_elements(UserLocator.dunning_status_names,doc)
         for i  in dunning_status_names:
              dunning_status_list.append(i.get_attribute('innerHTML'))
         time.sleep(5)
         return  dunning_status_list

    def  get_user_name(self):
         doc='获取用户名'
         user_name=self.get_element(UserLocator.user_name,doc).text
         time.sleep(5)
         return user_name

    def get_phone_number(self):
         doc='获取用户手机号'
         phone_number=self.get_element(UserLocator.phone_number,doc).text
         time.sleep(5)
         return  phone_number


    def get_user_group(self):
         doc='获取用户组'
         user_group=self.get_element(UserLocator.user_group,doc).text
         time.sleep(5)
         return   user_group

    def get_organization(self):
          doc='获取用户的组织'
          organization=self.get_element(UserLocator.orgazation,doc).text
          time.sleep(5)
          return organization

    def get_creation_time(self):
         doc='获取创建的时间'
         creation_time=self.get_element(UserLocator.creation_time,doc).text
         time.sleep(5)
         return creation_time

    def  get_action(self):
         doc='获取操作类型'
         time.sleep(5)
         self.click_element(UserLocator.reload_button,doc)
         self.wait_elevisble(OrganizationLocator.loading_wait,doc)
         self.wait_eleinvisble(OrganizationLocator.loading_wait,doc)
         self.wait_elevisble(UserLocator.action_button,doc)
         eles=self.get_elements(UserLocator.action_button,doc)
         eles[-1].click()
         time.sleep(3)
         operation_list=[]
         operations=self.get_elements(UserLocator.operation,doc)
         for i in operations:
             operation_list.append(i.get_attribute('innerHTML'))
         return  operation_list

    #系统重置
    def reset_user_password(self,email,cmdopt,get_database_source):
        doc='系统重置密码'
        time.sleep(5)
        self.refresh_user_page(Commondata.home_page[cmdopt]['user'])
        self.enter_the_user_page()
        self.query_user(email)
        self.click_element(UserLocator.reset_password_button,doc)
        time.sleep(2)
        #content=self.get_table_name(email,combot)
        #newpassword=self.get_reset_password(content)
        #return newpassword
        self.change_password_by_email(email,cmdopt,get_database_source)

    def admin_reset_org_user_pwd(self, email,cmdopt):
        doc = '主账号重置组织下面成员的密码'
        time.sleep(5)
        self.refresh_user_page(Commondata.home_page[cmdopt]['user'])
        self.enter_the_user_page()
        self.query_user(email)
        time.sleep(5)
        self.wait_elevisble(OrganizationLocator.reset_pwd, doc)
        self.move_mouse_to_click_element(OrganizationLocator.reset_pwd, doc)
        self.wait_elevisble(OrganizationLocator.delete_org_ack, doc)
        time.sleep(2)
        self.click_element(OrganizationLocator.delete_org_ack, doc)
        time.sleep(2)
        self.click_element(OrganizationLocator.edit_org_confirm, doc)
        self.wait_elevisble(OrganizationLocator.add_success_prompt, doc)

    #changepassword弹窗
    def change_password_by_email(self,email,cmdopt,get_database_source):
        doc='根据邮箱修改密码'
        time.sleep(5)
        self.refresh_user_page(Commondata.home_page[cmdopt]['user'])
        self.enter_the_user_page()
        self.query_user(email)
        self.click_element(UserLocator.reset_password_button,doc)
        time.sleep(2)
        self.wait_elevisble(UserLocator.change_pwd_send,doc)
        self.click_element(UserLocator.change_pwd_send,doc)
        time.sleep(2)
        change_pwd_verification_code=BusinessCommonTools().get_verification_code(cmdopt,email,get_database_source)
        time.sleep(2)
        self.send_keys(UserLocator.change_password_verificationcode_input,change_pwd_verification_code,doc)
        time.sleep(2)
        self.send_keys(UserLocator.new_pwd,Logindata.reset_pwd,doc)
        time.sleep(2)
        self.send_keys(UserLocator.confirm_new_pwd,Logindata.reset_confirm_pwd,doc)
        time.sleep(2)
        self.click_element(UserLocator.reset_pwd_ok,doc)
        self.wait_elevisble(Loginlocator.login_username_ele,doc)


    def get_reset_password(self,reset_pwd_content):
        doc='处理html'
        soup=BeautifulSoup(reset_pwd_content,"html.parser")
        strong_content=soup.find('strong').get_text()
        print(strong_content)
        new_pwd=strong_content.split(' ')[-1].replace('.','')
        return new_pwd


    def edit_org_info(self,email,cmdopt):
        doc='编辑组织信息'
        time.sleep(5)
        self.refresh_user_page(Commondata.home_page[cmdopt]['user'])
        self.enter_the_user_page()
        self.query_user(email)
        self.wait_elevisble(UserLocator.action_button,doc)
        self.click_element(UserLocator.action_button,doc)
        time.sleep(2)
        self.get_operation_name_click('Edit Organization')
        self.wait_elevisble(UserLocator.edit_org_select,doc)
        self.click_element(UserLocator.edit_org_select,doc)
        time.sleep(2)
        self.click_element(UserLocator().org_option_choose(Organization.default_organization[cmdopt]['user']),doc)
        self.click_element(UserLocator.delete_confirm,doc)
        self.wait_elevisble(OrganizationLocator.add_success_prompt,doc)


    def get_operation_name_click(self,operation):
        doc='获取操作名称然后点击'
        eles=self.get_elements(UserLocator.operation,doc)
        for ele in eles:
            if ele.text == operation:
                ele.click()

    def edit_user_group_info(self,email,usergroup,cmdopt):
        doc='编辑用户的用户组信息'
        time.sleep(5)
        self.refresh_user_page(Commondata.home_page[cmdopt]['user'])
        self.enter_the_user_page()
        self.query_user(email)
        self.wait_elevisble(UserLocator.action_button,doc)
        self.move_mouse_to_element(UserLocator.action_button)
        time.sleep(2)
        self.get_operation_name_click('Edit User Group')
        time.sleep(2)
        self.wait_elevisble(UserLocator.edit_user_group,doc)
        self.click_element(UserLocator.edit_user_group,doc)
        time.sleep(2)
        self.click_element(UserLocator().select_user_group(usergroup),doc)
        self.click_element(UserLocator.edit_user_group,doc)
        self.click_element(UserLocator.delete_confirm,doc)
        self.wait_elevisble(OrganizationLocator.add_success_prompt,doc)

    def edit_role_info(self,email,role,cmdopt):
        doc='编辑角色信息'
        time.sleep(5)
        self.refresh_user_page(Commondata.home_page[cmdopt]['user'])
        self.enter_the_user_page()
        self.query_user(email)
        #self.wait_elevisble(OrganizationLocator.loading_wait)
        self.wait_elevisble(UserLocator.action_button,doc)
        try:
            self.move_mouse_to_click_element(UserLocator.action_button,doc)
        except StaleElementReferenceException:
            ele=self.get_element(UserLocator.action_button,doc)
            ele.click()
        time.sleep(2)
        self.get_operation_name_click('Edit Role')
        self.wait_elevisble(UserLocator.edit_role,doc)
        self.click_element(UserLocator.edit_role,doc)
        time.sleep(2)
        self.click_element(UserLocator().select_role(role),doc)
        self.click_element(UserLocator.delete_confirm,doc)
        self.wait_elevisble(OrganizationLocator.add_success_prompt,doc)


    def lock_user(self,email,cmdopt):
        doc='锁定用户'
        time.sleep(5)
        self.refresh_user_page(Commondata.home_page[cmdopt]['user'])
        self.enter_the_user_page()
        self.query_user(email)
        try:
            self.move_mouse_to_click_element(UserLocator.action_button,doc)
        except StaleElementReferenceException:
            ele=self.get_element(UserLocator.action_button,doc)
            ele.click()
        time.sleep(2)
        self.get_operation_name_click('Lock')
        time.sleep(2)
        self.wait_elevisble(UserLocator.delete_acknowledge,doc)
        self.click_element(UserLocator.delete_acknowledge,doc)
        self.click_element(UserLocator.delete_confirm,doc)
        self.wait_elevisble(OrganizationLocator.add_success_prompt,doc)

    def unlock_user(self,email,cmdopt):
        doc='解锁用户'
        time.sleep(5)
        self.refresh_user_page(Commondata.home_page[cmdopt]['user'])
        self.enter_the_user_page()
        self.query_user(email)
        try:
            self.move_mouse_to_click_element(UserLocator.action_button,doc)
        except StaleElementReferenceException:
            ele = self.move_mouse_to_click_element(UserLocator.action_button,doc)
            ele.click()
        time.sleep(2)
        self.get_operation_name_click('Unlock')
        time.sleep(2)
        self.wait_elevisble(UserLocator.delete_acknowledge,doc)
        self.click_element(UserLocator.delete_acknowledge,doc)
        self.click_element(UserLocator.delete_confirm,doc)
        self.wait_elevisble(OrganizationLocator.add_success_prompt,doc)

    def resend_user(self,email,cmdopt):
        doc='重新激活用户'
        time.sleep(5)
        self.refresh_user_page(Commondata.home_page[cmdopt]['user'])
        self.enter_the_user_page()
        self.query_user(email)
        self.wait_elevisble(UserLocator.action_button,doc)
        time.sleep(2)
        self.click_element(UserLocator.action_button,doc)
        time.sleep(2)
        self.get_operation_name_click('Resend')
        #time.sleep(2)
        #self.wait_elevisble(UserLocator.delete_acknowledge)
        #self.click_element(UserLocator.delete_acknowledge)
        #self.click_element(UserLocator.delete_confirm)
        self.wait_elevisble(OrganizationLocator.add_success_prompt,doc)



